#region Imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using SearchCriteria = PHSRAG.Insight21.SearchContext.SearchCriteria;
using PHSRAG.Insight21.InsightUtilities;
#endregion

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// Abstracts the notion of a type of submissions on a protocol. It supports loading of the collection of
	/// all submissions of this type on a protocol.
	/// </summary>
	public class Submissions : BO.BusinessObject
	{
		#region Constants

		private const string SubmissionsKey = "Submissions_D5A518DD-42C8-4a0a-98FC-E29176396F1C";
		private const string SubmissionsTableName = "Submissions";

		#endregion

		#region Instance Variables (Private)

		// type of submissions the instance represents
		private string submissionType;

		#endregion

		#region Public Properties

		/// <summary>
		/// Get the total number of submissions of this type.
		/// </summary>
		public int TotalCount
		{
			get 
			{
				return DataContainer.Tables[SubmissionsTableName].Rows.Count; 
			}
		}

		#endregion

		/// <summary>
		/// Initialize the base class with the specified cache policy, and set the type of submission
		/// </summary>
		/// <param name="cachePolicy">policy to use to cache the instance</param>
		/// <param name="type">type of the submission to create</param>
		public Submissions(ICachePolicy cachePolicy, SubmissionType type) : base(cachePolicy)
		{
			switch( type )
			{
				case SubmissionType.InitialReview :
					submissionType = "Application";
					break;
				case SubmissionType.ContinuingReview :
					submissionType = "Continuing Review";
					break;
				case SubmissionType.Amendment :
					submissionType = "Amendment";
					break;
				case SubmissionType.AdverseEvent :
					submissionType = "Adverse Event";
					break;
				case SubmissionType.OtherEvent :
					submissionType = "Other Events";
					break;
                case SubmissionType.AnimalsIR :
                    submissionType = "Initial Review";
                    break;
                case SubmissionType.AnnualReview :
                    submissionType = "Annual Review";
                    break;
                case SubmissionType.Deficiency :
                    submissionType = "Deficiency";
                    break;
                case SubmissionType.TriennialReview :
                    submissionType = "Triennial Review";
                    break;
			}
		}


		#region BusinessObject Methods
		/// <summary>
		/// Cache the current instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(SubmissionsKey, this);
		}

		/// <summary>
		/// Remove the current instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(SubmissionsKey);
		}

		/// <summary>
		/// Load the current instance with a list of submission ids for the submission type
		/// </summary>
		/// <param name="connectionString">Database connection string</param>
		/// <param name="args">Is expected to contain a single argument - the protocol ID</param>
		public override void Load(
			string connectionString,  
			params object[] args)
		{
			if (args.Length != 1)
				throw new Exception("Submissions load requires a protocolID");

			try 
			{

				DataContainer =	DataAccess.GetDataSet(connectionString, 
					"GetProtocolSubmissions",
					DataAccess.BuildSqlParameterArray
					("@protocolId", SqlDbType.Int, (int)args[0],
					"@submissionType", SqlDbType.VarChar, submissionType));
				DataContainer.Tables[0].TableName = SubmissionsTableName;
			}

			catch(Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog(
					"Submissions Load", e, true, Utility.ExceptionReport.ReportType.Error);
				throw new Exception("Unable to load Submissions", e);
			}
		}

		#endregion

		#region Public Methods
		
		/// <summary>
		/// Creates all submission objects of the type the instance was constructed with, and stores
		/// then in sorted order based on the what number submission this is (of the given type)
		/// </summary>
		/// <param name="connectionString">string containing database connection info</param>
		/// <returns>The sorted list of submission objects</returns>
		public SortedList GetSubmissionList(string connectionString)
		{
			SortedList submissionList = new SortedList();

			DataTable dataTable = DataContainer.Tables[SubmissionsTableName];
			for (int i = 0; i < dataTable.Rows.Count; ++i)
			{
				Submission sub = new Submission(CachePolicy);
				sub.Load(connectionString, dataTable.Rows[i]["id"]);
				submissionList.Add(sub.SubmissionNumber, sub);
			}

			return submissionList;
		}

		#endregion

		/// <summary>
		/// Enumerates the types of possible submissions on a protocol
		/// </summary>
		public enum SubmissionType
		{
			InitialReview,
			ContinuingReview,
			Amendment,
			AdverseEvent,
			OtherEvent,
            AnimalsIR,
            AnnualReview,
            Deficiency,
            TriennialReview
		}
	}
}
